#include "triepart.h"
#include "ui_triepart.h"
#include <QMessageBox>
#include <QFile>
TriePart::TriePart(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TriePart)
{

    QFile files("D:\\Qt\\Documents\\DataStructureCourseDesign\\all.txt");
    files.open(QIODevice::ReadOnly);
    while(!files.atEnd()){
        QByteArray line = files.readLine();
        QString str(line);
        string s = str.toStdString();
        int len = s.size();
        string s1 = "",s2 = "";
        int i;
        for(i = 0; i < len && s[i] != ' '; ++i){
            if(str[i] != ' '){
                s1 += s[i];
            }
        }
        for(i++;i < len; ++i){
            s2 += s[i];
        }
        trie->insert(s1,s2);
    }

    ui->setupUi(this);
    this->setWindowTitle("英汉字典(HashTrie)");

    connect(ui->pushButton,&QPushButton::clicked,[=](){
       QString QKey = ui->textEdit->toPlainText();
       string key = QKey.toStdString();
       vector<trieTree> values = trie->search(key);
       ui->textEdit_3->setText("");
       if(values.size()){
           if(values[0]->key == key){
               ui->textEdit_2->setText(QString::fromStdString(values[0]->value));
           }else{
               ui->textEdit_2->setText(QString::fromStdString("NOT FOND!"));
               int len = values.size();
               ui->textEdit_3->insertPlainText("你可能要查找的单词：\n");
               for(int i =0;i < len; ++i){
                   QString keyI = QString::fromStdString(values[i]->key);
                   QString valueI = QString::fromStdString(values[i]->value);
                   ui->textEdit_3->insertPlainText(keyI + "\t" + valueI + '\n');
               }
           }
       }else{
           ui->textEdit_2->setText(QString::fromStdString("NOT FOND!"));
           QMessageBox::information(this,"查找单词","未找到该单词！");
       }
    });

    //清除
    connect(ui->pushButton_2,&QPushButton::clicked,this,[=](){
        ui->textEdit_2->setText(tr(""));
        ui->textEdit->setText(tr(""));
        ui->textEdit_3->setText("");
    });

    //插入
    connect(ui->pushButton_3,&QPushButton::clicked,[=](){
        QString s1 = ui->textEdit->toPlainText();
        QString s2 = ui->textEdit_2->toPlainText();
        ui->textEdit_3->setText("");
        if(s1.size() == 0|| s2.size() == 0){
            QMessageBox::information(this,"插入单词","插入单词错误！");
        }
        if(trie->insert(s1.toStdString(),s2.toStdString())){
            QMessageBox::information(this,"插入单词","新建单词成功！");
        }else{
            QMessageBox::information(this,"插入单词","插入重复单词！");
        }
    });

    connect(ui->pushButton_4,&QPushButton::clicked,[=](){
        QString s = ui->textEdit->toPlainText();
        ui->textEdit_3->setText("");
        if(s.size() == 0){
            QMessageBox::information(this,"插入单词","删除单词错误！");
        }
        if(trie->deleteWord(s.toStdString())){
            QMessageBox::information(this,"删除单词","成功删除！");
        }else{
            QMessageBox::information(this,"删除单词","删除失败，未找到该单词！");
        }
    });
}

TriePart::~TriePart()
{
    delete ui;
}
